//
//  MyHistMenuTableViewCell.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2020/9/9.
//  Copyright © 2020 moon. All rights reserved.
//

#import "MyHistMenuTableViewCell.h"
#import "MenuCollectionViewCellModel.h"
@implementation MyHistMenuTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.gridLayout = [[MKUIGridPageCollectionViewLayout alloc] init];
        self.gridLayout.enableCycleScroll = YES;
        self.gridLayout.sectionInset = UIEdgeInsetsMake(10, 10, 20, 10);
        self.gridLayout.interitemSpacing = 10;
        self.gridLayout.lineSpacing = 10;
        self.gridLayout.itemSize = CGSizeMake(60, 60);
        self.collectionView = [[MKUICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.gridLayout];
//        self.collectionView.pagingEnabled = YES;
        self.collectionView.decelerationRate = UIScrollViewDecelerationRateFast;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        [self.cellContentView addSubview:self.collectionView];
        //
        self.collectionView.model.forwardDelegate = self;
        //
        self.pageControl = [[UIPageControl alloc] init];
        [self.cellContentView addSubview:self.pageControl];
        [self colorTraitCollectionDidChange];
    }
    return self;
}
- (void)colorTraitCollectionDidChange{
    [super colorTraitCollectionDidChange];
    self.pageControl.pageIndicatorTintColor = [UIColor mk_colorWithLight:[UIColor grayColor]];
    self.pageControl.currentPageIndicatorTintColor = [UIColor systemBlueColor];
    self.collectionView.backgroundColor = [UIColor mk_colorWithLight:[UIColor whiteColor]];
}
- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    CGRect bounds = self.cellContentView.bounds;
    CGRect f1 = bounds;
    self.collectionView.frame = f1;
    //
    [self __updatePageControl];
    [self layoutPageControl];
}
- (void)layoutPageControl{
    CGRect bounds = self.cellContentView.bounds;
    CGRect f2 = bounds;
    f2.size = [self.pageControl sizeThatFits:bounds.size];
    f2.origin.y = CGRectGetMaxY(bounds)-f2.size.height/2 -7;
    f2.origin.x = CGRectGetMinX(bounds)+(bounds.size.width-f2.size.width
                                         )/2;
    self.pageControl.frame = f2;
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
//    MyMenuInfo *modelObject = customCellModel.modelObject;
    [self __reloadData];
}
- (void)__reloadData{
    [self.collectionView.model removeAllSectionModels];
    MyMenuInfo *info = self.cellModel.modelValue;
    for (Menu *menu in info.histMenus) {
        MenuCollectionViewCellModel *cm = [[MenuCollectionViewCellModel alloc] initWithModelObject:menu];
        [self.collectionView.model addCellModel:cm];
    }
    [self.collectionView.model reloadCollectionViewData];
    [self.collectionView layoutIfNeeded];
    [self __updatePageControl];
}
- (void)__updatePageControl{
    self.pageControl.numberOfPages = self.gridLayout.numberOfPages;
    self.pageControl.currentPage = self.gridLayout.currentPage;
}
- (CGSize)customSizeThatFits:(CGSize)size{
    UIEdgeInsets insets = [self.class contentInsets];
    CGSize itemSize = self.gridLayout.itemSize;
    UIEdgeInsets setionInset = self.gridLayout.sectionInset;
    return CGSizeMake(size.width, itemSize.height+insets.top+insets.bottom+setionInset.top+setionInset.bottom);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView gridPageLayout:(MKUIGridPageCollectionViewLayout *)collectionViewLayout interitemSpacingForSectionAtIndex:(NSInteger)section{
    return [collectionViewLayout interitemSpacingThatFitsItemSize:collectionViewLayout.itemSize minimumInteritemSpacing:collectionViewLayout.interitemSpacing sectionInsets:collectionViewLayout.sectionInset];
}
- (void)tableView:(UITableView *)tableView willDisplayCellModel:(__kindof MKUITableViewCellModel *)cellModel{
    [self.gridLayout startAutoScrollingWithDistance:1 duration:2];
}
- (void)tableView:(UITableView *)tableView didEndDisplayingCellModel:(__kindof MKUITableViewCellModel *)cellModel{
    [self.gridLayout stopAutoScrolling];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self __updatePageControl];
}
@end
